package com.training.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.utils.StringUtils;
import com.training.admin.dao.IdmUserDao;
import com.training.admin.dao.ProjectDataDao;
import com.training.admin.dao.TraineeDao;
import com.training.admin.entity.project.ProjectTrainee;
import com.training.admin.entity.trainee.IdmUser;
import com.training.admin.entity.trainee.Trainee;
import com.training.admin.handler.api.TraineeApiHandler;
import com.training.admin.service.TraineeService;
/**
 * 
 * @author xiaoHU
 *
 */
@Service("traineeService")
public class TraineeServiceImpl extends DataServiceImpl<Trainee> implements TraineeService{
    
    @Autowired
    private TraineeApiHandler traineeApiHandler;
    
    @Dao
    private TraineeDao traineeDao;
    
    @Dao
    private ProjectDataDao projectDataDao;
    
    @Dao
    private IdmUserDao idmUserDao;

    /* (non-Javadoc)
     * @see com.training.admin.service.TraineeService#saveAllIdmUser()
     */
    @SuppressWarnings("unchecked")
    public void saveAllIdmUser() {
        
        // delete all idm user
        idmUserDao.deleteAll();
        
        HttpResult httpResult = traineeApiHandler.getTrainees();
        
        Map<String, List<Map<String, String>>> maps = (Map<String, List<Map<String, String>>>) httpResult.getData().get("idmUsers");
        List<IdmUser> idmUsersItem = new ArrayList<IdmUser>();
        for (Entry<String, List<Map<String, String>>> entry : maps.entrySet()) {
            for (Map<String, String> innerEntry : entry.getValue()) {
                String json = JsonUtils.toJson(innerEntry);
                IdmUser idmUser = JsonUtils.parse(json, IdmUser.class);
                idmUsersItem.add(idmUser);
                genericDaoSupport.save(idmUser);
            }
        }
        
        Map<String, IdmUser> idmUserMap = CollectionUtils.constructMapValue(idmUsersItem, "mail");
        
        List<Trainee> trainees = genericDaoSupport.loadAll(Trainee.class);
        for (Trainee trainee : trainees) {
            IdmUser exitIdmUser = idmUserMap.get(trainee.getEmail());
            if (exitIdmUser != null) {
                genericDaoSupport.update(trainee.updateByIdmUser(exitIdmUser));
            }
        }
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.TraineeService#getTraineeCodeAndName(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, String>> listIdmUser(String query, Long currentPorjectId) {
        
        String search = StringUtils.connect("%", query, "%");
        List<IdmUser> idmUsers = idmUserDao.listBySearch(search, search, search, search);
        Map<String, Trainee> filterTrainees = new HashMap<String, Trainee>();
        
        if(currentPorjectId != null) {
            List<ProjectTrainee> projectTrainees = projectDataDao.listProjectTraineeByProjectid(currentPorjectId);
            List<Trainee> filterTraineesList = CollectionUtils.extractPropertyList(projectTrainees, "trainee");
            filterTrainees = CollectionUtils.constructMapValue(filterTraineesList, "email");
        }
        
        List<Map<String, String>> listMap = new ArrayList<>();
        for(IdmUser idmUser : idmUsers) {
            
            String item = idmUser.getUserorgnamefullpath();
            String result ="";
            String [] strings = item.split("-");
            for (int i = 2; i<strings.length; i++) {
                if (i != strings.length-1) {
                    result += strings[i] + "-";
                }else {
                    result += strings[i];
                }
            }
//            int indexOf = idmUser.getUserorgnamefullpath().indexOf("-", 3);
//            String result = idmUser.getUserorgnamefullpath().substring(indexOf);
            
            if(filterTrainees.get(idmUser.getMail()) == null) {
                Map<String, String> map = new HashMap<String,String>();
                map.put("id", idmUser.getId().toString());
                map.put("text", idmUser.getUsercn() + "-" + result);
                listMap.add(map);
            }
        }
        
        return listMap;
    }

    /* (non-Javadoc)
     * @see com.training.admin.service.TraineeService#loadByCode(java.lang.String)
     */
    public Trainee loadByCode(String code) {
        return traineeDao.loadByCode(code);
    }

}
